package com.itheima.service.Impl;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpVO;
import com.itheima.pojo.UserQuery;
import com.itheima.service.EmpService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Service;

import javax.management.remote.rmi.RMIServerImpl;
import java.util.List;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper,Emp> implements EmpService {
/*    需求：改造根据id修改用户余额的接口，要求如下
    1.完成对用户状态校验
    2.完成对用户余额校验
    3.如果扣减后余额为0，则将用户status修改为冻结状态(2)*/
    @Override
    public void deductMoney(Integer id, Integer money) {
        Emp emp = getById(id);
        if(emp.getStatus()==2||emp==null){
            throw new RuntimeException("用户状态异常");
        }
        if(emp.getBalance()<money){
            throw new RuntimeException("用户余额不足");
        }
        int remain=emp.getBalance()-money;
        lambdaUpdate().set(remain==0,Emp::getStatus,2)
                .set(Emp::getBalance,remain)
                .eq(Emp::getBalance,emp.getBalance())
                .eq(Emp::getId,id)
                .update();

    }

//    复杂的多条件查询
    @Override
    public List<Emp> queryUsers(String name, Integer status, Integer minBalance, Integer maxBalance) {
        List<Emp> list = lambdaQuery().like(null != name, Emp::getUsername, name)
                .gt(null != minBalance, Emp::getBalance, minBalance)
                .lt(null != maxBalance, Emp::getBalance, maxBalance)
                .eq(null != status, Emp::getStatus, status)
                .list();
        return list;
    }


}
